package com.company.链表;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangjian
 * @data 2023/2/26
 * @Version 1.0
 * @Deceripion 反转链表
 */
public class AB10反转链表 {
    public static void main(String[] args) {
        Solution1 solution = new Solution1();
    }
}


class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

class Solution1 {

    private ListNode pHead = new ListNode(1);

    public ListNode ReverseList(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            stack.push(head);
            head = head.next;
        }
        if (stack.isEmpty()) {
            return null;
        }
        // 定义头结点
        ListNode node = stack.pop();
        ListNode dummy = node;
        //栈中的结点全部出栈，然后重新连成一个新的链表
        while (!stack.isEmpty()) {
            dummy.next = stack.pop();
            dummy = dummy.next;
        }
        //最后一个结点就是反转前的头结点，一定要让他的next
        dummy.next = null;
        return node;
    }

    /**
     * 添加节点元素
     *
     * @param node
     */
    public void addNode(ListNode node) {
        ListNode temp = pHead;
        while (true) {
            if (temp.next != null) {
                temp = temp.next;
            } else {
                temp.next = node;
                break;
            }
        }
    }

    public int checkNode(int value) {
        ListNode temp = pHead;
        if (temp.next != null) {
            while (true) {
                if (temp.val == value) {
                    return temp.val;
                } else {
                    temp = temp.next;
                }
            }
        }

        return 0;
    }

}

